package jin.chen.service.impl;

import jin.chen.mapper.CarouselMapper;
import jin.chen.mapper.UserAddressMapper;
import jin.chen.pojo.Carousel;
import jin.chen.pojo.UserAddress;
import jin.chen.pojo.bo.AddressBo;
import jin.chen.service.AddressService;
import jin.chen.service.CarouselService;
import jin.chen.utils.enums.YesOrNotDefault;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    private UserAddressMapper userAddressMapper;
    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<UserAddress> queryAllAddress(String userId) {
        UserAddress us = new UserAddress();
        us.setUserId(userId);
        return userAddressMapper.select(us);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addAddress(AddressBo addressBo) {
        //先查看是否有默认收获地址
        int isDefault = 0;
        List<UserAddress> addressList = queryAllAddress(addressBo.getUserId());
        if(addressList == null || addressList.size() == 0 ){
            isDefault = 1;
        }
        UserAddress ua = new UserAddress();
        BeanUtils.copyProperties(addressBo, ua);
        String id = sid.nextShort();
        ua.setId(id);
        ua.setCreatedTime(new Date());
        ua.setUpdatedTime(new Date());
        ua.setIsDefault(isDefault);
        userAddressMapper.insert(ua);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateAddress(AddressBo addressBo) {
        String addressId = addressBo.getAddressId();
        UserAddress ua = new UserAddress();
        ua.setId(addressId);
        BeanUtils.copyProperties(addressBo, ua);
        ua.setUpdatedTime(new Date());
        userAddressMapper.updateByPrimaryKeySelective(ua);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteAddress(String userId, String addressId) {
        Example example = new Example(UserAddress.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("id", addressId);
        userAddressMapper.deleteByExample(example);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void setDefaultAddress(String userId, String addressId) {
        //先找出默认地址，更新为非默认
        UserAddress queryAddress = new UserAddress();
        queryAddress.setUserId(userId);
        queryAddress.setIsDefault(YesOrNotDefault.YES.getValue());
        List<UserAddress> list = userAddressMapper.select(queryAddress);
        for(UserAddress ua : list){
            ua.setIsDefault(YesOrNotDefault.NO.getValue());
            userAddressMapper.updateByPrimaryKeySelective(ua);
        }

        //再更新传入地址为更新
        UserAddress defaultAddress = new UserAddress();
        defaultAddress.setUserId(userId);
        defaultAddress.setId(addressId);
        defaultAddress.setIsDefault(YesOrNotDefault.YES.getValue());
        userAddressMapper.updateByPrimaryKeySelective(defaultAddress);
    }
}
